"""
A class to exercise the JT core functions.
"""
import itk
import vtk

import connect2
import ImageCompare


def Test_ImageOrientation():
    """
    VTK image flipped about its Y axis is the same as
    an unmodified ITK image or a VTK image converted to ITK, but not flipped."
    """
    import connect2
    import ImageCompare

    inputFilename='C:/Users/bryan/bryan-code/2D3D/vert1/fluoro/ushortim080-LAT.mha'
    outPath = 'C:/Users/bryan/bryan-code/trunk/Tests/'

    vtk2itk = connect2.vtk2itk()
    #
    # ITK Pipeline
    # ############
    input_image = itk.Image.F2.New()
    output_image = itk.Image.US2.New()

    itk_reader = itk.ImageFileReader[input_image].New()

    itk_reader.SetFileName(inputFilename)
    itk_reader.Update()

    itk_caster = itk.CastImageFilter[input_image,output_image].New()
    itk_caster.SetInput(itk_reader.GetOutput())
    itk_caster.Update()

    itk_SaveName = outPath + 'TestImageOrientation_itkOut.png'

    itk_writer = itk.ImageFileWriter[output_image].New()
    itk_writer.SetInput(itk_caster.GetOutput())
    itk_writer.SetFileName(itk_SaveName)
    itk_writer.Write()

    #
    # VTK Pipeline
    # ############
    vtk_reader = vtk.vtkMetaImageReader()
    vtk_caster = vtk.vtkImageCast()
    vtk_writer = vtk.vtkPNGWriter()
    vtk_writer2 = vtk.vtkPNGWriter()
    vtk_flipper = vtk.vtkImageFlip()

    vtk_reader.SetFileName(inputFilename)
    vtk_reader.Update()

    vtk_caster.SetInput(vtk_reader.GetOutput())
    vtk_caster.SetOutputScalarTypeToUnsignedShort()
    vtk_caster.Update()

    vtk_flipper.SetFilteredAxis(1)
    vtk_flipper.SetInput(vtk_caster.GetOutput())

    viewer = vtk.vtkImageViewer()
    viewer.SetInput(vtk_flipper.GetOutput())
    vtk_flipper.Update()

    vtk_SaveName = outPath + 'TestImageOrientation_vtkFlipOut.png'
    vtk_SaveName2 = outPath + 'TestImageOrientation_vtkUnFlipOut.png'

    vtk_writer.SetInput(vtk_flipper.GetOutput())
    vtk_writer.SetFileName(vtk_SaveName)
    vtk_writer.Write()

    vtk_writer2.SetInput(vtk_caster.GetOutput())
    vtk_writer2.SetFileName(vtk_SaveName2)
    vtk_writer2.Write()

    #
    # VTK->ITK Pipeline
    # ############
    vtk2itk.SetInput(vtk_caster.GetOutput())
    vtk2itk_SaveName = outPath + 'TestImageOrientation_vtk2itkOut.png'
    itk_writer.SetFileName(vtk2itk_SaveName)
    itk_writer.SetInput(vtk2itk.GetOutput())
    itk_writer.Write()

    #Test for positive match

    #return 0 if they match, 1 otherwise.
    assert ImageCompare.compare(itk_SaveName,vtk_SaveName)==0
    assert ImageCompare.compare(itk_SaveName,vtk2itk_SaveName)==0
    print "Test Success!  VTK image flipped about its Y axis is the same as \
            an unmodified ITK image or a VTK image converted to ITK, but not flipped."



if __name__ == "__main__":
    Test_ImageOrientation()